import { ApiProperty } from "@nestjs/swagger";
import { BeforeInsert, BeforeUpdate, Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Classify } from "../classify/classify.entity";
import { Tag } from "../tag/tag.entity";

@Entity()
export class Article {
    @PrimaryGeneratedColumn({
        comment:'主键id'
    })
    id: number;

    @Column({
        comment:'标题'
    })
    title: string;

    @Column({
        nullable: true,
        comment:'描述'
    })
    desc: string;

    @Column({
        nullable: true,
        comment:'封面'
    })
    cover: string;

    @Column({
        type: 'text',
        comment:'内容'
    })
    content: string;

    @Column({
        comment:'排序'
    })
    sort: number;

    @Column({
        type: 'tinyint',
        comment:'状态 1显示 0隐藏'
    })
    status: number;

    @Column({
        type: 'bigint',
        comment:'浏览量'
    })
    view: number;

    @Column({
        type: 'bigint',
        update:false,
        comment:'创建时间'
    })
    create_time: number;

    @Column({
        type: 'bigint',
        comment:'修改时间'
    })
    update_time: number;

    @BeforeInsert()
    beforeInsert() {
        this.create_time = new Date().getTime();
        this.update_time = new Date().getTime();
        this.view = 0;
    }

    @BeforeUpdate()
    beforeUpdate() {
        this.update_time = new Date().getTime();
    }

    @ManyToOne(() => Classify, (classify) => classify.articles, {
        onDelete: "CASCADE"
    })
    @JoinColumn({name:'classify_id'})
    classify: Classify;

    @ManyToMany(() => Tag, (tags) => tags.articles, {
        onDelete: "CASCADE"
    })
    @JoinTable({ 
        name: 'article_tag',
        joinColumn: {
            name: "a_id",
            referencedColumnName: "id"
        },
        inverseJoinColumn: {
            name: "t_id",
            referencedColumnName: "id"
        }
     })
    tags: Tag[];
}
